/*eslint-disable */
@Inject('$log', '$rootScope', '$scope', '$state', 'previewTableService', 'processTableService', 'box', 'ngDialog')
class ProcessTableDetailController {
  constructor() {
    this.view = {
      isReadonly: true,
      key: null,
    };

    this.boxType = [];
    if (box.children && box.children.length > 0) {
      this.boxType = box.children;
    } else {
      this.boxType.push(box);
    }

    this.tableId = $state.params.tableId;
    this.boxId = box.id;
    this.piId = this.$state.params.piId;

    this.data = {
      contractingUnit: '', // 承包单位
      supervisionUnit: '', // 监理单位
      contractNo: '', // 合同号
      no: '', // 编号
      projectName: '', // 项目名称
      auditContentName: '', // 审查内容名称
      superiorAdvice: '', // 监理审查意见
      viceEngineer: '', // 安全副总监/专业监理工程师
      viceEngineerSignTime: '', // 安全副总监/专业监理工程师签字时间
      viceEngineerSignTimeStr: '',
      engineerAdvice: '', // 总监理工程师意见
      engineer: '', // 总监理工程师
      engineerSignTime: '', // 总监理工程师签字时间
    };

    // 获取模板图片id
    this.$getDetailTable();
    this.$getProcess();

  }

  // 获取流程
  $getProcess() {
    this.mapParams = {
      piId: this.$state.params.piId,
    }
    const mapParams = this.mapParams;
    this.processTableService.queryFlowNode({mapParams}).then((res) => {
      if (res.success) {
        this.processList = res.rows;
        this.processList.forEach((item, index) => {
          if (item.nodeKey === 'end' && item.currentNode) {
            this.isShowBtn = true;
          } else {
            this.isShowBtn = false;
          }
        });
      }
    });
  }

  // 查询表格数据
  $getDetailTable() {
    const id = this.$state.params.tableId;
    this.processTableService
      .get(id)
      .then((res) => {
        if (res.success) {
          this.data = res.data;
        }
      });
  }

  // 流转记录
  $transferRecordDialog(ctrl) {
    this.ngDialog.open({
      template: 'transferRecord',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog record-dialog',
      controllerAs: 'vm',
      controller($rootScope, $scope, $state, ngDialogService) {
        'ngInject';

        this.$table = {
          fetchTimes: 0,
          isTableLoading: false,
          pagination: {
            ITEMS_PER_PAGE: 5,
            MAX_SIZE: 5,
            currentPage: 1,
            total: 0,
          },
        };
        this.query = () => {
          ctrl.processTableService
          .queryConfirmList({
            mapParams: { piId: ctrl.piId },
            page: this.$table.pagination.currentPage,
            size: this.$table.pagination.ITEMS_PER_PAGE,
          })
          .then((res) => {
            if (res.success) {
              this.recordList = res.rows;
              this.$table.pagination.totalItems = res.total;
              this.$table.pagination.total = res.total;
              this.$table.isTableLoading = false;
              if (res.total < 1) {
                this.$table.fetchTimes += 1;
              }
            } else {
              ngDialogService.alertError(res.message);
            }
          });
        }
        this.query();
      },
    });
  }

  // 资料归档
  $mobileDocument(ctrl) {
    this.ngDialog.open({
      template: 'dataArchiving',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($rootScope, $scope, ngDialogService, documentService) {
        'ngInject';

        this.view = {
          attachmentFiles: [],
        };

        this.boxType = angular.copy(ctrl.boxType);

        this.document = {
          orgId: $rootScope.orgId || storageApi.get('orgId'),
          documentName: '',
          documentNumber: '',
          compileTime: '',
          remark: '',
          attachmentIds: [],
          documentSourceId: ctrl.tableId,
          piId: ctrl.piId,
          type: ctrl.boxId,
          documentSourceType: 2,
        };

        this.document.compileTime = new Date();

        this.submitForm = () => {
          this.document.attachmentIds = this.view.attachmentFiles.map(item => item.id);
          documentService.save(this.document)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess(res.message);
                $scope.closeThisDialog();
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }

  $print() {
    $('.qr-print-container').print({ globalStyles: false, timeout: 1200 });
    // $('.qr-print').jqprint();
  }

}

export default ProcessTableDetailController;
